package dbu.dbase;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * The records follow the header in the table file. 
 * Data records are preceded by one byte, that is, a space (20h) if the record is not deleted, an asterisk (2Ah) if the record is deleted. 
 * Fields are packed into records without field separators orrecord terminators. 
 * The end of the file is marked by a single byte, with the end-of-file marker, an OEM code page character value of 26 (1Ah).
 * @author orlin
 *
 */
public class TableRecords extends DBaseStructure {

	private DBaseHeader header;
	private List<DBaseRecord> records;

	public TableRecords(DBaseHeader header) {
		this.header = header;
		records = new ArrayList<DBaseRecord>();
	}

	public List<DBaseRecord> getRecords() {
		return records;
	}

	@Override
	protected int read(InputStream stream) throws IOException {
		//System.out.println(header);
		for (int i = 0; i < header.getNumberOfRecords(); i++) {
			DBaseRecord record = new DBaseRecord(header);
			record.read(stream);
			//System.out.println(record);
			records.add(record);
		}
		/** EOF */
		readByte(stream);
		return header.getNumberOfRecords() * header.getNumberOfBytesInTheRecord() + 1;
	}

}
